package com.jzdjt.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jzdjt.entity.PaperEntity;
import com.jzdjt.tool.DBUtil;

public class UpdateObjectPaperAnswerServlet extends HttpServlet {

	public UpdateObjectPaperAnswerServlet() {
		super();
	}

	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		response.setCharacterEncoding("UTF-8");

		// 根据标示名获取JSP文件中表单所包含的参数
		String stuid = request.getParameter("stuid");
		String answer = request.getParameter("answer");
		String paperid = request.getParameter("paperid");
		String grade = request.getParameter("grade");
		String subject = request.getParameter("subject");
		boolean result = updateSql(stuid, paperid, grade, subject, answer);

		PrintWriter out = response.getWriter();// 回应请求

		out.write(result + "");
		out.flush();
		out.close();
		System.out.println(result);
	}

	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//doGet(request, response);
		// 设置客户端的解码方式为utf-8
		response.setContentType("text/html;charset=utf-8");
		response.setCharacterEncoding("UTF-8");

		// 根据标示名获取JSP文件中表单所包含的参数
		String stuid = request.getParameter("stuid");
		String answer = request.getParameter("answer");
		String paperid = request.getParameter("paperid");
		String grade = request.getParameter("grade");
		String subject = request.getParameter("subject");
		boolean result = updateSql(stuid, paperid, grade, subject, answer);

		PrintWriter out = response.getWriter();// 回应请求

		out.write(result + "");
		out.flush();
		out.close();
		System.out.println(result);
	}

	private boolean updateSql(String stuid, String paperid, String grade, String subject, String answer) {
		boolean rets = false;

		try {
			Date d = new Date(); // 获取当前系统的时间
			// 格式化日期
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
			String answerTime = sdf.format(d); // 转为字符串
			

            String selectsql = "select * from ObjAnswer where paperID='"+paperid+"' and studentID='"+stuid+"'";
            DBUtil dbUtil = new DBUtil();
			List<String> getlist = dbUtil.select(selectsql) ;
			if("".equals(answer)){
				String selectsqlans = "select * from PaperInfo where paperID='"+paperid+"'";
				List<PaperEntity> getAnslist = dbUtil.QueryFromPaper(selectsqlans) ;
				if(getAnslist!=null&&getAnslist.size()>0){
					PaperEntity p = getAnslist.get(0);
					if(!"".equals(p.getAnswer())){
						int count = Integer.parseInt(p.getAnswer().toString()) ;
						if(count>0){
							for(int i=0;i<count;i++){
								answer += "#*";
							}
							answer = answer.substring(0,answer.length()-1);
						}
					}
				}
			}
			String insertSubjectAnsSql = "insert into ObjAnswer(paperID,studentID,answer,answerTime) values('" + paperid
					+ "','" + stuid + "','" + answer + "','" + answerTime + "')";

            if(getlist!=null&&getlist.size()>0){
            	insertSubjectAnsSql = "update objAnswer set answer = '"+answer+"',answerTime='"+answerTime+"' where paperID='"+paperid+"' and studentID='"+stuid+"'";
            }
			List<PaperEntity> getPaper = getSubject(paperid);
			
			if (getPaper!=null&&getPaper.get(0).getAnswer() != null) {
				String solution = getPaper.get(0).getAnswer().toString();
				if (solution.contains("*")) {
					String[] ansarry = solution.split("\\*");
					String[] useransarry = answer.split("\\*");
					String[] erroinfoSqls = new String[ansarry.length];
					for (int j = 0; j < ansarry.length; j++) {
						String ansnode = ansarry[j];
						String useransnode = useransarry[j];
						if (!ansnode.equals(useransnode)) {
							// 导入错题集
							erroinfoSqls[j] = "insert into PaperWrongRecord(StudentID,QuestionNum,AnswerTime,PaperGrade,PaperSubject,PaperID,SelectedChoice) values('" + stuid + "','第" + (j + 1) + "题','"
									+ answerTime + "','" + grade + "','" + subject + "','" + paperid + "','"+useransnode+"')" + ";";
						}
					}
					
					System.out.println("insertSubjectAnsSql:"+insertSubjectAnsSql);
					System.out.println("erroinfoSql:"+erroinfoSqls.toString());
					rets = dbUtil.excuteSql(insertSubjectAnsSql);
					String deleteSql = "delete from PaperWrongRecord where studentID='"+stuid+"' and PaperID='"+paperid+"'";
					rets = dbUtil.excuteSql(deleteSql);
					rets = dbUtil.excuteSqlByBatch(erroinfoSqls);
					rets = true;
				}
				else{
					rets = false;
					System.out.println("答案异常！"+solution);
				} 
			}
			else{
				rets = false;
				System.out.println("答案为空异常！");
			}
			
			
			
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			rets = false;
			System.out.println(e.getMessage());
			//e.printStackTrace();
			return false;
		} 
		return rets;
	}

	private List<PaperEntity> getSubject(String paperid) {
		String getPaperSql = "select * from PaperInfo where PaperID = '" + paperid + "' ";
		List<PaperEntity> paper = null;
		try {
			DBUtil dbUtil = new DBUtil();
			paper = dbUtil.QueryFromPaper(getPaperSql);
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println(e.getMessage());
		}
		
		return paper;
	}

}